/**
 * 后台登陆js
 */
var loginJs={
	startEvent:function(){
		//跳转到注册页面
		$('#gotoRegister').click(function(){
			location.href = ThinkPHP['MODULE'] + '/Registered/register';
		});

		//点击更换验证码
		var verifyimg = $('#codeImg').attr('src');
		$('#shuaxin').click(function () {
			if (verifyimg.indexOf('?') > 0) {
				$('#codeImg').attr('src', verifyimg + '&random=' + Math.random());
			}
			else {
				$('#codeImg').attr('src', verifyimg + '?random=' + Math.random());
			}
		});
		//按回车键登录事件
		document.onkeydown = function(e){ 
			var ev = document.all ? window.event : e;
			if(ev.keyCode==13) {
				var username=$.trim($('#username').val());
				if(username==''){
					$('#alert-warning').css('display','block');
					$('#alertMsg').html('用户名不能为空！');
					return false;
				}
				var password=$.trim($('#password').val());
				if(password==''){
					$('#alert-warning').css('display','block');
					$('#alertMsg').html('密码不能为空！');
					return false;
				}
				loginJs.login();
	 		}
		};
	},
	
	bindEvent:function(){
		$('#btn-login').unbind();
		$('#btn-login').on('click',function(){
			var username=$.trim($('#username').val());
			if(username==''){
				$('#alert-warning').css('display','block');
				$('#alertMsg').html('用户名不能为空！');
				return false;
			}
			var password=$.trim($('#password').val());
			if(password==''){
				$('#alert-warning').css('display','block');
				$('#alertMsg').html('密码不能为空！');
				return false;
			}
			loginJs.login();
		});
	},
	
	login:function(){
		$('#alert-warning').css('display','none');
		var params = {
			'name':$('#username').val(),
			'pwd':$('#password').val()
		};
		
		/*$('#loginDiv').html('<div style="width:100%;text-align:center;color:red;">正在登陆，请稍后...</div>');*/
		var url= '../login';
		var cb=function(data){
			if(data.resultCode!=200){
				$('#alert-warning').css('display','block');
				$('#alertMsg').html(data.msg);
				
				/*//刷新验证码
				if(data.resultCode==300){
					var verifyimg = $('#codeImg').attr('src');
					$('#codeImg').attr('src', verifyimg + '&random=' + Math.random());
				}
				var input='<button class="btn btn-warning btn-lg btn-block" id="btn-login" type="submit">登&nbsp;陆</button>';
				$('#loginDiv').html(input);*/
				loginJs.bindEvent();
			}else{
				//跳转到后台管理页面
				location.href = '/Index/adminIndex';
			}
		};
		sendApiNoLoad(url,params,cb,'POST');
	},
	
	init:function(){
		loginJs.bindEvent();
		loginJs.startEvent();
	}
}
$(document).ready(function(){
	loginJs.init();
});

